package cn.yangliu.nacos.consumer.web;

import javax.annotation.PostConstruct;

import cn.yangliu.nacos.comm.JsonResult;
import cn.yangliu.nacos.comm.tools.ResponseUtils;
import cn.yangliu.nacos.consumer.feign.Oauth2ClientInfo;
import cn.yangliu.nacos.consumer.feign.Oauth2Service;
import cn.yangliu.nacos.consumer.feign.Provider;
import cn.yangliu.nacos.consumer.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 问道于盲
 * @date 2019-08-18
 */
@RestController
public class ConsumerController {

    @Autowired
    private Oauth2Service oauth2Service;

    @PostConstruct
    public void init(){
        JsonResult<Oauth2ClientInfo> jsonResult = oauth2Service.clientInfo(
                "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" +
                        ".eyJzY29wZSI6WyJhbGwiXSwiZXhwIjoxNTc4NjM3NTgxLCJqdGkiOiIwYTE5MzY1Ni05NDQ1LTRjZWUtOGMwZC02NjkzMzZmNmY5ZGIiLCJjbGllbnRfaWQiOiJhZG1pbiJ9.Z56fijtDXhqPBZlz5w59nc6mvWAqBIX8KO4kjv07MA4");
        System.out.println(jsonResult);

        String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1Nzg2Mzc0MDAsInVzZXJfbmFtZSI6ImFkbWluIiwianRpIjoiODY4MTkwMzItMzNhYy00NzcwLTkxZTktZTVkNjZjYjdmZWUwIiwiY2xpZW50X2lkIjoiYWRtaW4iLCJzY29wZSI6WyJhbGwiXX0.jeLlCGEK8Ngk3q018e7FKvoWYJ96CFIxYy2TtUdtdHk";
        token = "Bearer " + token;
        JsonResult<UserModel> result = oauth2Service.principal(token);
        System.out.println(result);

    }

    @GetMapping("/clientInfo")
    public Oauth2ClientInfo clientInfo(String token){
        JsonResult<Oauth2ClientInfo> result = oauth2Service.clientInfo(token);
        return ResponseUtils.checkResult(result);
    }

    @GetMapping("/principal")
    public UserModel principal(String token){
        token = "Bearer " + token;
        JsonResult<UserModel> result = oauth2Service.principal(token);
        return ResponseUtils.checkResult(result);
    }

    @Autowired
    private Provider provider;

    @GetMapping("/test")
    public Object test(){
        return provider.test();
    }
}
